package com.yx.convertor;

import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.LocalDateTimeTypeHandler;
import org.springframework.stereotype.Component;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.time.LocalDateTime;

@Component
public class LocalDateTimeConvertor extends LocalDateTimeTypeHandler {
    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, LocalDateTime parameter, JdbcType jdbcType) throws SQLException {
        Timestamp timestamp=Timestamp.valueOf(parameter);
        ps.setObject(i,timestamp);
    }

    @Override
    public LocalDateTime getNullableResult(ResultSet rs, String clumnName) throws SQLException {
        Timestamp timestamp=rs.getTimestamp(clumnName);
        if(null==timestamp){
            return  null;
        }
        return timestamp.toLocalDateTime();
    }
}
